import { Message } from 'element-ui'
import { mapGetters } from 'vuex'

/*
export const isValidUsername = (str: string) => {
    return ['admin', 'editor'].indexOf(str.trim()) >= 0
}
*/
export const isValidUsername = (str: string) => {
    // console.log('state_roles: ', ...mapGetters([
    //     'roles'
    // ]))
    // console.log('state_roles: ', mapGetters(map: string[]):Dictionary<Computed>)
    return str.trim().length > 1
}

export const isExternal = (path: string) => /^(https?:|mailto:|tel:)/.test(path)

export const isArray = (arg: any) => {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/* 必填，不为空 */
export const isRequired = (key: string, value: any) => {
    if (value.toString().trim() === '') {
        Message({
            message: `${key}不能为空！`,
            type: 'error',
            duration: 2000
        })
        return false
    }
    return true
}

export const isValidURL = (url: string) => {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}
